
import re, util, json, csv
from BeautifulSoup import BeautifulStoneSoup
from StringIO import StringIO

def dictsToCsv(dicts, filename):
    keys = []
    for dict in dicts:
        for key in dict.keys():
            if not keys.__contains__(key):
                keys.append(key)
    
    #print(keys)
    
    finalList = []
    
    for dict in dicts:
        entry = []
        for key in keys:
            val = dict.get(key)
            if not val == None:
                entry.append(val)
            else:
                entry.append("")
        finalList.append(entry)   
            
    #print(finalList)        
    
    
    file = open(filename, "w")
    writer = csv.writer(file)
    writer.writerow(keys)
    writer.writerows(finalList)
    file.close()
    


listFile = open('rooms.txt', 'r')
lines = listFile.readlines()

baseUrl = "http://ims.fas.harvard.edu/classrooms/room.php?rm=rm"

io = StringIO()

# Open file for writing
file = open('output.txt', 'w')
rooms = []

for line in lines:
    room = {}
    url = baseUrl + line[:4]
    soup = util.mysoupopen(url)
    
    bldg = soup.findAll("h1")
    room['id'] = line[:4]
    room['bldg'] = bldg[0].contents[0].strip()
    room['room'] = bldg[0].contents[3].contents[0]
    
    table = soup.findAll("table")
    table = table[0]
    #print(table.contents)
    for row in table.contents:
        if row != u'\n':
            prop = row.contents[0].contents[0].strip().strip(":")
            m = re.search("<td>([^<]*)", str(row.contents[1]))
            val = m.group(1)
            room[prop] = val

    photo = soup.find("img", {"id":"room_photo"})
    photo = photo["src"]
    
    if photo == "http://www.fas.harvard.edu/~ims/Class/images/nophoto.jpg":
        photo = ""
    
    room['photo'] = photo
    rooms.append(room)
    print(room)    

file.close()

dictsToCsv(rooms, "rooms.csv")


